﻿using DBVisitor.Core;
using DBVisitor.DBProvider;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DBVisitor.DBProvider
{
    public class QueryReader : IDisposable,ICloneable
	{
		public IDbConnection Connection { get; protected set; }
		public IDataReader Reader { get; protected set; }
		private QueryReaderBackup Backup { get; set; }
		public IDBFieldAccessor FieldAccessor { get; set; }

		public QueryReader(IDbConnection conn, IDataReader reader, QueryReaderBackup backup, IDBFieldAccessor dbAccess)
		{
			Connection = conn;
			Reader = reader;
			Backup = backup;
			FieldAccessor = dbAccess;
		}

		public object Clone()
		{
			return Backup.CreateReader();
		}

		~QueryReader()
		{
			Dispose(false);
		}

		public void Dispose()
		{
			Dispose(true);
			GC.SuppressFinalize(this);
		}

		protected virtual void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (Reader != null)
				{
					Reader.Dispose();
					Reader = null;
				}
				if (Connection != null)
				{
					Connection.Dispose();
					Connection = null;
				}
			}
		}
		
		public bool Read()
		{
			if (Reader == null)
				return false;
			return Reader.Read();
		}

		public T Get<T>(Column column)
		{
			if (Reader == null)
				return default(T);
			return new DBValue<T>(column, FieldAccessor, Reader).Deserialize();
		}
    }
}
